package ru.dadle8.service.impl.command.workout;

import org.springframework.stereotype.Service;
import org.telegram.telegrambots.meta.api.methods.ForwardMessage;
import org.telegram.telegrambots.meta.api.objects.Message;
import org.telegram.telegrambots.meta.api.objects.Update;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import ru.dadle8.model.bot.BotConsts;
import ru.dadle8.model.bot.type.CommandState;
import ru.dadle8.model.db.Workout;
import ru.dadle8.service.impl.command.AbstractCommandService;

import java.util.List;

@Service
public class FindWorkoutCommandServiceImpl extends AbstractCommandService {

    @Override
    public CommandState getCommandState() {
        return CommandState.FIND_WORKOUT;
    }

    @Override
    public void processUpdate(Update update) throws TelegramApiException {
        List<Workout> workouts = getWorkoutService().findWorkouts(getInMess().getText());
        for (Workout workout : workouts) {
            ForwardMessage forwardMessage = ForwardMessage.builder()
                    .messageId(workout.getMessageId())
                    .chatId(getChatId())
                    .fromChatId(BotConsts.CHANNEL_WITH_WORKOUTS).build();
            getBot().execute(forwardMessage);
        }

        getStorage().clearState(getChatId());
    }
}
